package com.ebt.m.view;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

import com.ebt.m.R;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.widget.BaseAlertDialog;

/**
 * 保险年龄选择框
 *
 * @author allen.li
 */
public class DialogInsuranceAgePicker extends BaseAlertDialog {

    public static final int AGE_PICKER_MIN = 0;
    public static final int AGE_PICKER_MAX = ConfigData.MAX_AGE;
    private NumberPicker picker;

    private int mInitAge = 30;

    public DialogInsuranceAgePicker(Context context, int initAge, int minAge, int maxAge) {
        super(context);

        View view = LayoutInflater.from(context).inflate(R.layout.common_customer_age_picker, null);
        builder.setView(view);

        picker = (NumberPicker) view.findViewById(R.id.common_customer_age_picker);
        int length = maxAge - minAge + 1;
        String[] displayedValues = new String[length];
        for (int i = 0; i < length; i++) {
            displayedValues[i] = String.valueOf(minAge + i);
        }
        // 设置np2的最小值和最大值
        picker.setMinValue(minAge);
        picker.setMaxValue(maxAge);
        picker.setDisplayedValues(displayedValues);

        // 设置np2的当前值
        picker.setValue(initAge);
        picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

        builder.setPositiveButton(R.string.sure, this);
        builder.setNegativeButton(R.string.cancel, this);

    }

    public DialogInsuranceAgePicker(Context context, int initAge) {
        super(context);

        View view = LayoutInflater.from(context).inflate(R.layout.common_customer_age_picker, null);
        builder.setView(view);

        picker = (NumberPicker) view.findViewById(R.id.common_customer_age_picker);

        String[] displayedValues = new String[AGE_PICKER_MAX + 1];
        for (int i = 0; i < AGE_PICKER_MAX + 1; i++) {
            displayedValues[i] = String.valueOf(i);
        }

        // 设置np2的最小值和最大值
        picker.setMinValue(AGE_PICKER_MIN);
        picker.setMaxValue(AGE_PICKER_MAX);
        picker.setDisplayedValues(displayedValues);

        // 设置np2的当前值
        picker.setValue(initAge);
        picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

        builder.setPositiveButton(R.string.sure, this);
        builder.setNegativeButton(R.string.cancel, this);

    }

    @Override
    protected String getTitle() {
        return "请选择年龄";
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                if (mListener != null) {
                    mListener.onConfirmed(picker.getValue());
                }
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                if (mListener != null) {
                }
                break;
            default:
                break;
        }
    }

}
